﻿using System;
using Newtonsoft.Json.Linq;

namespace Neo4jConnect
{   
    public abstract class Neo4jElement : PropertyContainer
    {
        public Uri SelfUri { get; protected set; }
     
        public ulong Id 
        {             
            get
            {
                if (SelfUri == null)
                    throw new Neo4jException("SelfUri is not set");
                return ulong.Parse(SelfUri.Segments[SelfUri.Segments.Length - 1]);
            }
        }
                        
        private GraphDatabaseService _graphDatabase;

        protected GraphDatabaseService GraphDatabase
        {
            get
            {
                if (_graphDatabase == null)
                    throw new Exception("GraphDatabase is not initialized");

                return _graphDatabase;
            }
        }
        
        internal void Init(GraphDatabaseService graphDatabase)
        {
            _graphDatabase = graphDatabase;
        }

        internal void ReadJson(JToken obj)
        {
            OnReadJson(obj);
        }
        
        protected abstract void OnReadJson(JToken obj);

        public abstract void Save();
        public abstract void Delete();
    }
}
